package com.epfresh.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.OfferListActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.utils.T;
import com.epfresh.bean.CartGood;
import com.epfresh.bean.CartGoods;
import com.epfresh.bean.OfferDtoBean;
import com.epfresh.global.BaseApplication;
import com.epfresh.utils.FormatUtil;
import com.epfresh.views.tag.TagCloudView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;


/**
 * Created by wzh on 2016/5/6.
 */
public class CartExpandListAdapter extends BaseExpandableListAdapter implements View.OnClickListener {
    int colors[] = {Color.RED, Color.parseColor("#434343"), Color.parseColor("#999999"), Color.parseColor("#24af75"), Color.parseColor("#f7fff6"), Color.parseColor("#f8f8f8")};//价格激活红色，失活黑色横线
//    private CartGoods goodss;

    //红 黑 灰
    public interface OnCheckChangeListener {
        public void onCheckChange(boolean isCheck, int groupPosition, int childPosition);
    }

    public interface OnTextChangeListener {
        public void onTextChange(int groupPosition, int childPosition, double lastCnt, CartGood cartGood);
    }

    FreshItemListener freshItemListener;

    public interface FreshItemListener {
        void freshItem(int type, int position, CartGood goods, View v);
    }

    public FreshItemListener getFreshItemListener() {
        return freshItemListener;
    }

    public void setFreshItemListener(FreshItemListener freshItemListener) {
        this.freshItemListener = freshItemListener;
    }

    public Context context;
    public ExpandableListView lv;

    public List<CartGoods> groupList;


    private AdapterView.OnItemClickListener onItemClickListener;
    private OnBottomClickListener onBottomClickListener;
    private OnNotifyDataSetChangedListener onNotifyDataSetChangedListener;
    private OnCheckChangeListener onCheckChangeListener;
    private OnTextChangeListener onTextChangeListener;
    private OnTextChangeListener onChangeListener;
    private OnCountListener onCountListener;

    private boolean isEdit;

    //是否是问价后降价商品
//    private boolean isShowOfferPrice=false;
    //是否是促销或秒杀商品
    private boolean isSecondKillOrPromotion = false;

//    private EdUnitWatch edUnitWatch;


    public OnCountListener getOnCountListener() {
        return onCountListener;
    }

    public void setOnCountListener(OnCountListener onCountListener) {
        this.onCountListener = onCountListener;
    }

    public CartExpandListAdapter(Context context, ExpandableListView lv, List<CartGoods> groupList) {
        this.context = context;
        this.groupList = groupList;
        this.lv = lv;
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public OnTextChangeListener getOnTextChangeListener() {
        return onTextChangeListener;
    }

    public void setOnTextChangeListener(OnTextChangeListener onTextChangeListener) {
        this.onTextChangeListener = onTextChangeListener;
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return onCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public OnTextChangeListener getOnChangeListener() {
        return onChangeListener;
    }

    public void setOnChangeListener(OnTextChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    @Override
    public void notifyDataSetChanged() {
        if (onNotifyDataSetChangedListener != null) {
            onNotifyDataSetChangedListener.OnNotifyDataSetChangedListener(groupList.size());
        }
        expendList();
        super.notifyDataSetChanged();
    }


    public OnNotifyDataSetChangedListener getOnNotifyDataSetChangedListener() {
        return onNotifyDataSetChangedListener;
    }

    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener onNotifyDataSetChangedListener) {
        this.onNotifyDataSetChangedListener = onNotifyDataSetChangedListener;
    }

    public OnBottomClickListener getOnBottomClickListener() {
        return onBottomClickListener;
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int count = 0;
        CartGoods cartGoods = groupList.get(groupPosition);
        List<CartGood> configs = cartGoods.getResult();

        if (configs != null) {
            count = configs.size();
        }
        if (!cartGoods.isDisabled() && count > 3 && !cartGoods.isExpend()) {
            count = 3;
        }


        return count;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return groupList.get(groupPosition).getResult().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_list_cart, null, false);

            vh.iv = (ImageView) convertView.findViewById(R.id.iv_box);
            vh.tv = (TextView) convertView.findViewById(R.id.tv_name);
            vh.tvOffer = (TextView) convertView.findViewById(R.id.tv_offer);
            vh.vLine = convertView.findViewById(R.id.v_line);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }


        convertView.setTag(R.id.item_key_position, groupPosition);
        convertView.setClickable(true);

        CartGoods cartGoods = groupList.get(groupPosition);

        if (cartGoods.isDisabled()) {
            cartGoods.setSelect(false);
            vh.iv.setVisibility(View.GONE);
            vh.tv.setVisibility(View.GONE);
        } else {
            vh.iv.setVisibility(View.VISIBLE);
            vh.tv.setVisibility(View.VISIBLE);
        }
        if (cartGoods.isShowOfferBtn()) {
            vh.tvOffer.setVisibility(View.VISIBLE);
        } else {
            vh.tvOffer.setVisibility(View.GONE);
        }
        if (cartGoods.getResult() == null || cartGoods.getResult().size() == 0) {
            vh.vLine.setVisibility(View.GONE);
        } else {
            vh.vLine.setVisibility(View.VISIBLE);

        }

        checkBoxState(cartGoods.isSelect(), vh.iv);

//        if (cartGoods.getWholeSalesType() == 1) {
//            vh.tv.setText(cartGoods.getWholeSaleMarketName());
//        } else {
//            vh.tv.setText(cartGoods.getDistributionTitle());
//        }
        vh.tv.setText(cartGoods.getStoreOrMarketName());
        vh.iv.setTag(R.id.item_key_position, groupPosition);
        vh.iv.setOnClickListener(this);

        vh.tvOffer.setTag(R.id.item_key_position, groupPosition);
        vh.tvOffer.setOnClickListener(this);
        return convertView;
    }


    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        convertView = getChildNormalView(groupPosition, childPosition, isLastChild, convertView, parent);


        return convertView;
    }

    @SuppressLint("SetTextI18n")
    public View getChildNormalView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder vh = null;


        if (convertView == null) {
            int[] a = {1};
            int b = a.length;
            vh = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_child_list_cart_copy, null, false);
            vh.tv_car_offer_hint = convertView.findViewById(R.id.tv_car_offer_hint);
            vh.ivChildCheck = (ImageView) convertView.findViewById(R.id.iv_child_box);
            vh.ivLogo = (ImageView) convertView.findViewById(R.id.iv_logo);
            //现货,预售标志
            vh.tvType = (TextView) convertView.findViewById(R.id.tv_type);
            vh.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            vh.tvStore = (TextView) convertView.findViewById(R.id.tv_store);

            vh.tvUnit = (TextView) convertView.findViewById(R.id.tv_unit_package);

            //vh.tvUnitPrice = (TextView) convertView.findViewById(R.id.tv_unit);
            vh.tvOutOfCity = (TextView) convertView.findViewById(R.id.tv_out_of_city);
            vh.tvOutOfDate = (TextView) convertView.findViewById(R.id.tv_out_of_date);
            vh.ivOutOfCity = (ImageView) convertView.findViewById(R.id.iv_out_of_city);


            vh.ivAdd = (ImageView) convertView.findViewById(R.id.iv_add);
            vh.ivRemove = (ImageView) convertView.findViewById(R.id.iv_remove);

            vh.edAmount = (TextView) convertView.findViewById(R.id.ed_price);
            vh.edAmountUnderlineLayout = (LinearLayout) convertView.findViewById(R.id.item_child_list_ed_price_underline_layout);
            vh.edAmountUnderlineTv = (TextView) convertView.findViewById(R.id.item_child_list_ed_price_underline_tv);
            vh.tvDisable = (TextView) convertView.findViewById(R.id.tv_disable);
            vh.vBacker = convertView.findViewById(R.id.v_backer);
            vh.ivCorner = (ImageView) convertView.findViewById(R.id.iv_corner);

            vh.tvTotal = (TextView) convertView.findViewById(R.id.tv_total);
            vh.tvTotalName = (TextView) convertView.findViewById(R.id.tv_total_name);
            vh.tvCount = (TextView) convertView.findViewById(R.id.tv_btn);
            vh.vCount = convertView.findViewById(R.id.rl_count);

            vh.tvMsg = (TextView) convertView.findViewById(R.id.tv_msg);
            vh.vLine = convertView.findViewById(R.id.v_line_2);
            vh.vFlag = convertView.findViewById(R.id.iv_msg);
            vh.vArrow = convertView.findViewById(R.id.tv_msg_right);

            vh.vLine1 = convertView.findViewById(R.id.v_line_1);
            vh.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            vh.tv_price_ = (TextView) convertView.findViewById(R.id.tv_price_);
            vh.tv_price_old = (TextView) convertView.findViewById(R.id.tv_price_old);
            vh.tv_activity_promotion = convertView.findViewById(R.id.tv_activity_promotion);
            vh.tv_activity_second = convertView.findViewById(R.id.tv_activity_second);
            vh.tvChargeNumber = (TextView) convertView.findViewById(R.id.tv_charge_number);


            vh.gif_1 = convertView.findViewById(R.id.gif_1);
            vh.gif_2 = convertView.findViewById(R.id.gif_2);
            vh.gif_3 = convertView.findViewById(R.id.gif_3);
            vh.gif_4 = convertView.findViewById(R.id.gif_4);
            vh.ll_offer = convertView.findViewById(R.id.ll_offer);
            vh.tv_offer_price = (TextView) convertView.findViewById(R.id.tv_offer_price);
            vh.tv_offer_hint = convertView.findViewById(R.id.tv_offer_hint);
            vh.tv_offer_time_remind = convertView.findViewById(R.id.tv_offer_time_remind);

            vh.tv_starting_account = convertView.findViewById(R.id.tv_starting_account);
            vh.tv_difference = convertView.findViewById(R.id.tv_difference);

            convertView.setTag(vh);
        } else {
            vh = (ChildViewHolder) convertView.getTag();
        }


        CartGoods cartGoods = groupList.get(groupPosition);

//        goodss = groupList.get(groupPosition);
        List<CartGood> goods = cartGoods.getResult();
        CartGood cartGood = goods.get(childPosition);

        vh.tv_car_offer_hint.setText(cartGood.getOfferText());

        checkBoxState(cartGood.isSelect(), vh.ivChildCheck);

        if (cartGoods.getWholeSalesType() == 1) {
            vh.tvStore.setText(cartGood.getStoreName());
            vh.tvStore.setVisibility(View.VISIBLE);
        } else {
            vh.tvStore.setVisibility(View.GONE);
        }
//        vh.tv_price.setText(cartGood.getLastestPriceUnit());//"¥"+cartGood.getProductUnitPrice()+"/"+cartGood.getProductChargeUnit());
//        vh.tv_price.setText(cartGood.getProductPriceUnit());//"¥"+cartGood.getProductUnitPrice()+"/"+cartGood.getProductChargeUnit());


        //vh.tv_price_.setText("元/"+cartGood.getProductChargeUnit());

        String productTitle = cartGood.getProductTitle();
        if (productTitle == null) {
            productTitle = "";
        }

/*        double productUnitPrice = cartGood.getProductUnitPrice();
        vh.tvUnitPrice.setText(context.getResources().getString(R.string.total_price,productUnitPrice)+'/'+cartGood.getProductChargeUnit());*/
        String productPackageSize = cartGood.getProductPackageSize();
        if (productPackageSize == null || "".equals(productPackageSize)) {
            vh.tvUnit.setText("");
            vh.tvUnit.setVisibility(View.GONE);
        } else {
            if (cartGood.getSpecName() == null || cartGood.getSpecName().equals("默认")) {
                vh.tvUnit.setText(productPackageSize);
            } else {
                vh.tvUnit.setText(cartGood.getSpecName() + "(" + productPackageSize + ")");
            }
            vh.tvUnit.setVisibility(View.VISIBLE);
        }
        if (cartGood.isPrePrice()) {
            vh.tv_price.setText("¥" + cartGood.getLastestUnitPrice() + "/" + cartGood.getProductChargeUnit());//重新设置显示原价
        } else {
            vh.tv_price.setText("时价");//重新设置显示原价
        }

/*

        Log.e("留言:","cnt:"+(cartGood.getMerMsg()!=null&&!"".equals(cartGood.getMerMsg())&&cartGoods.getWholeSalesType()==2));
        Log.e("留言:getWholeSalesType","cnt:"+(cartGoods.getWholeSalesType()==2));
*/

        if (cartGood.getMerMsg() != null && !"".equals(cartGood.getMerMsg()))//&&cartGoods.getWholeSalesType()==2)
            vh.tvMsg.setText(cartGood.getMerMsg());
        else {
            vh.tvMsg.setText("去留言");
        }
        vh.tv_car_offer_hint.setVisibility(View.VISIBLE);
        vh.tv_price.setVisibility(View.VISIBLE);
        vh.tv_price_.setVisibility(View.VISIBLE);
        vh.tv_price_old.setVisibility(View.VISIBLE);

        vh.tv_price.setTextColor(colors[0]);
        vh.tv_price.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

        vh.ll_offer.setVisibility(View.GONE);
        vh.tv_offer_time_remind.setVisibility(View.GONE);
        boolean disabled = cartGoods.isDisabled();
        if (!disabled) {
            //vh.tvUnitPrice.setVisibility(View.VISIBLE);
            vh.tvDisable.setVisibility(View.GONE);
            vh.tvOutOfDate.setVisibility(View.GONE);
            if (cartGood.isCheckCityId()) {//正常情况
                vh.ivChildCheck.setVisibility(View.VISIBLE);
                vh.ivChildCheck.setEnabled(true);
                vh.tvOutOfCity.setVisibility(View.GONE);
                vh.ivOutOfCity.setVisibility(View.GONE);
                vh.ivAdd.setVisibility(View.VISIBLE);
                vh.ivRemove.setVisibility(View.VISIBLE);
                vh.edAmount.setVisibility(View.VISIBLE);
                vh.edAmountUnderlineLayout.setVisibility(View.VISIBLE);
                vh.tvChargeNumber.setVisibility(View.VISIBLE);
                vh.vBacker.setVisibility(View.VISIBLE);
                convertView.setBackgroundColor(Color.WHITE);

                if (cartGoods.getWholeSalesType() == 2) {//||cartGoods.getWholeSalesType()==1){
                    vh.vLine1.setVisibility(View.VISIBLE);
                    vh.tvMsg.setVisibility(View.VISIBLE);
                    vh.vFlag.setVisibility(View.VISIBLE);
                    vh.vLine.setVisibility(View.VISIBLE);
                    vh.vArrow.setVisibility(View.VISIBLE);

                } else {
                    vh.vLine1.setVisibility(View.GONE);
                    vh.tvMsg.setVisibility(View.GONE);
                    vh.vFlag.setVisibility(View.GONE);
                    vh.vLine.setVisibility(View.GONE);
                    vh.vArrow.setVisibility(View.GONE);
                    if (cartGood.getOfferProductDto() != null) {
                        initState(vh, cartGood);
                    }
                }
            } else {
                if (isEdit) {
                    vh.ivChildCheck.setVisibility(View.VISIBLE);
                    vh.ivChildCheck.setEnabled(true);
                } else {
                    cartGood.setSelect(false);
                    vh.ivChildCheck.setVisibility(View.INVISIBLE);
                    vh.ivChildCheck.setEnabled(false);
                }
                vh.tvOutOfCity.setVisibility(View.VISIBLE);
                vh.ivOutOfCity.setVisibility(View.VISIBLE);
                vh.ivAdd.setVisibility(View.GONE);
                vh.ivRemove.setVisibility(View.GONE);
                vh.edAmount.setVisibility(View.GONE);
                vh.edAmountUnderlineLayout.setVisibility(View.GONE);
                vh.vBacker.setVisibility(View.GONE);
                vh.tvOutOfCity.setText(context.getString(R.string.only_area, cartGood.getCityName()));
                convertView.setBackgroundColor(Color.parseColor("#f9f9f9"));
                vh.vLine1.setVisibility(View.GONE);
                vh.tvChargeNumber.setVisibility(View.GONE);

                vh.tvMsg.setVisibility(View.GONE);
                vh.vFlag.setVisibility(View.GONE);
                vh.vLine.setVisibility(View.GONE);
                vh.vArrow.setVisibility(View.GONE);

            }


        } else {
            if (isEdit) {
                vh.ivChildCheck.setVisibility(View.VISIBLE);
                vh.ivChildCheck.setEnabled(true);
            } else {
                vh.ivChildCheck.setVisibility(View.INVISIBLE);
                vh.ivChildCheck.setEnabled(false);
            }
            vh.tvUnit.setVisibility(View.INVISIBLE);
            //vh.tvUnitPrice.setVisibility(View.INVISIBLE);
            vh.tvOutOfCity.setVisibility(View.GONE);
            vh.ivOutOfCity.setVisibility(View.GONE);
//            vh.ivChildCheck.setVisibility(View.GONE);
            vh.tvOutOfDate.setVisibility(View.VISIBLE);
            vh.tv_car_offer_hint.setVisibility(View.GONE);
            vh.tv_price.setVisibility(View.GONE);
            vh.tv_price_.setVisibility(View.GONE);
            vh.tv_price_old.setVisibility(View.GONE);
            vh.tvChargeNumber.setVisibility(View.GONE);
            vh.tvDisable.setVisibility(View.VISIBLE);
            vh.ivAdd.setVisibility(View.GONE);
            vh.vBacker.setVisibility(View.GONE);
            vh.ivRemove.setVisibility(View.GONE);
            vh.edAmount.setVisibility(View.GONE);
            vh.edAmountUnderlineLayout.setVisibility(View.GONE);
            convertView.setBackgroundColor(Color.WHITE);

            vh.vLine1.setVisibility(View.GONE);

            vh.tvMsg.setVisibility(View.GONE);
            vh.vFlag.setVisibility(View.GONE);
            vh.vLine.setVisibility(View.GONE);
            vh.vArrow.setVisibility(View.GONE);
        }


        if (childPosition == goods.size() - 1) {
//            vh.vLine.setVisibility(View.INVISIBLE);
        } else {
//            vh.vLine.setVisibility(View.VISIBLE);
        }

        vh.ivChildCheck.setTag(R.id.item_key_position, groupPosition);
        vh.ivChildCheck.setTag(R.id.item_key_status, childPosition);
        vh.ivChildCheck.setOnClickListener(this);

        vh.ivAdd.setTag(R.id.item_key_position, groupPosition);
        vh.ivAdd.setTag(R.id.item_key_status, childPosition);
        vh.ivAdd.setTag(R.id.item_key_view, vh.edAmount);
        vh.ivAdd.setOnClickListener(this);

        vh.ivRemove.setTag(R.id.item_key_position, groupPosition);
        vh.ivRemove.setTag(R.id.item_key_status, childPosition);
        vh.ivRemove.setTag(R.id.item_key_view, vh.edAmount);
        vh.ivRemove.setOnClickListener(this);

        vh.edAmount.setTag(R.id.item_key_position, groupPosition);
        vh.edAmount.setTag(R.id.item_key_status, childPosition);


        vh.edAmount.setText(FormatUtil.subZeroAndDot(cartGood.getProductCount() + ""));
        vh.edAmountUnderlineTv.setText(FormatUtil.subZeroAndDot(cartGood.getProductCount() + ""));
        vh.tvChargeNumber.setText("已选" + FormatUtil.subZeroAndDot(cartGood.getProductCount() + "") + cartGood.getProductChargeUnit() + "");
        vh.edAmount.setOnClickListener(this);
        vh.vMore = convertView.findViewById(R.id.rl_more);
        vh.ivMore = (ImageView) convertView.findViewById(R.id.iv_more);
        vh.tvMore = (TextView) convertView.findViewById(R.id.tv_more);

        vh.tvMsg.setTag(R.id.item_key_position, groupPosition * 1000 + childPosition);
        //vh.tvMsg.setTag(R.id.item_key_status, childPosition);
        vh.tvMsg.setTag(R.id.item_key_status, cartGood);
        vh.tvMsg.setOnClickListener(this);


        ImageLoader.getInstance().displayImage(cartGood.getProductImgUrl(), vh.ivLogo);


        if ("2".equals(cartGood.getSalesType())) {//预售
            vh.tvType.setVisibility(View.VISIBLE);
//            vh.tvTitle.setText("               "+productTitle);

            String html = "<font color=\"#00ffffff\">【预售】" + productTitle + "</font>";
            vh.tvTitle.setText(Html.fromHtml(html));

        } else {

            vh.tvType.setVisibility(View.GONE);
            vh.tvTitle.setText(productTitle);
        }


        convertView.setTag(R.id.item_key_position, groupPosition);
        convertView.setTag(R.id.item_key_status, childPosition);
        convertView.setOnClickListener(onClickListener);
        convertView.setOnLongClickListener(onLongClickListener);
        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                mRawX = event.getRawX();
                mRawY = event.getRawY();

                return false;
            }
        });

        //促销秒杀设置显示
        String promotionType = cartGood.getPromotionType();
        if ("1".equals(promotionType) && !disabled) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.VISIBLE);
            vh.tv_activity_second.setVisibility(View.GONE);
            if (cartGood.isPrePrice()) {
                vh.tv_price.setText("¥" + cartGood.getProductUnitPrice() + "/" + cartGood.getProductChargeUnit());//重新设置显示原价
                vh.tvUnit.setVisibility(View.VISIBLE);
            } else {
                vh.tv_price.setText("时价");//重新设置显示原价
                vh.tvUnit.setVisibility(View.GONE);
            }
            isSecondKillOrPromotion = true;
        } else if ("2".equals(promotionType) && !disabled) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.VISIBLE);
            if (cartGood.isPrePrice()) {
                vh.tvUnit.setVisibility(View.VISIBLE);
                vh.tv_price.setText("¥" + cartGood.getProductUnitPrice() + "/" + cartGood.getProductChargeUnit());//重新设置显示原价
            } else {
                vh.tv_price.setText("时价");//重新设置显示原价
                vh.tvUnit.setVisibility(View.GONE);
            }
            isSecondKillOrPromotion = true;
        } else {
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.GONE);

            isSecondKillOrPromotion = false;
        }
        String promotionMarkUrl = cartGood.getMarkUrl();
        ImageLoader.getInstance().displayImage(promotionMarkUrl, new ImageViewAware(vh.ivCorner), BaseApplication.getTagOptions(promotionMarkUrl), null, null, null);
        boolean isLast = false;
        int count = 0;
        if (goods != null) {
            count = goods.size();
        }

        if (count > 3 && !cartGoods.isExpend()) {
            if (childPosition == 2) {
                isLast = true;
            }
        } else {
            if (childPosition == goods.size() - 1) {
                isLast = true;
            }
        }
        if (isLast && !disabled) {
            if (goods.size() > 3) {
                vh.vMore.setVisibility(View.VISIBLE);
            } else {
                vh.vMore.setVisibility(View.GONE);
            }
        } else {
            vh.vMore.setVisibility(View.GONE);
        }

        if (!cartGoods.isExpend()) {
            int cnt = goods.size() - 3;
            cnt = cnt > 0 ? cnt : 0;
            vh.tvMore.setText("查看其余" + cnt + "件商品");
            vh.ivMore.setImageResource(R.mipmap.down_arrow);
        } else {
            vh.tvMore.setText("收起列表");
            vh.ivMore.setImageResource(R.mipmap.up_arrow);
        }

        vh.tvMore.setTag(R.id.item_key_view, cartGoods);
        vh.tvMore.setOnClickListener(onMoreClickListener);
        vh.ivMore.setTag(R.id.item_key_view, cartGoods);
        vh.ivMore.setOnClickListener(onMoreClickListener);


//        if (vh.tv_offer_time_remind.getVisibility()==View.VISIBLE){
//            isShowOfferPrice=true;
//        }else {
//           isShowOfferPrice=false;
//        }

        if (isLast && !disabled) {
            vh.vCount.setVisibility(View.VISIBLE);
            Pair<Integer, Double> selectTotalPrice = getSelectTotalPrice(cartGoods);

            int selectCount = selectTotalPrice.first;

            double totalPrice = selectTotalPrice.second == null ? 0 : selectTotalPrice.second;

            boolean isUnable = false;
            vh.tvTotal.setText(context.getString(R.string.total_price, totalPrice));


            if (selectCount > 0) {
                vh.tvCount.setText("结算(" + selectCount + ")");
                vh.tvCount.setBackgroundResource(R.drawable.btn_count_normal);
                isUnable = false;
            } else {
                vh.tvCount.setBackgroundResource(R.drawable.btn_count_unable);
                vh.tvCount.setText("结算");

                isUnable = true;
            }
            if (isEdit) {
                vh.tvCount.setBackgroundResource(R.drawable.btn_count_unable);
                vh.tvTotalName.setVisibility(View.GONE);
                vh.tvTotal.setVisibility(View.GONE);
                vh.tvCount.setText("结算");
                vh.tvCount.setEnabled(false);
                isUnable = true;
            } else {

                vh.tvTotalName.setVisibility(View.VISIBLE);
                vh.tvTotal.setVisibility(View.VISIBLE);
                vh.tvCount.setEnabled(true);
            }
            vh.tvCount.setTag(R.id.item_key_status, isUnable);
            vh.tvCount.setTag(R.id.item_key_view, cartGoods);
            vh.tvCount.setOnClickListener(onCountClickListener);
        } else {
            vh.vCount.setVisibility(View.GONE);
        }
        if (cartGood.isPrePrice()) {
            vh.tvTotalName.setVisibility(View.VISIBLE);
            vh.tvTotal.setVisibility(View.VISIBLE);
        } else {
            vh.tvTotalName.setVisibility(View.GONE);
            vh.tvTotal.setVisibility(View.GONE);
        }

        String groupKey = cartGoods.getGroupKey();
        if (groupKey.startsWith("20")) {
            vh.tv_starting_account.setText(cartGoods.getDeliveryMinAmount() + "元起送");
            String startAccount = cartGoods.getDeliveryMinAmount();
            if (startAccount == null || startAccount.equals("0") || startAccount.equals("")) {
                vh.tv_starting_account.setVisibility(View.GONE);
                vh.tv_difference.setVisibility(View.GONE);
            } else {
                vh.tv_starting_account.setVisibility(View.VISIBLE);
                vh.tv_difference.setVisibility(View.VISIBLE);
                String s = vh.tvTotal.getText().toString();
                String totalAccount = s.substring(0, s.length() - 1);

                BigDecimal diffAccount = new BigDecimal(startAccount).subtract(new BigDecimal(totalAccount)).setScale(2, BigDecimal.ROUND_HALF_UP);
                double doubleValue = diffAccount.doubleValue();
                if (doubleValue > 0) {
                    vh.tv_difference.setVisibility(View.VISIBLE);
                    vh.tv_difference.setText(Html.fromHtml("还差<font color='#FF9E16'>" + diffAccount.toString() + "</font>元"));
                    vh.tvCount.setEnabled(false);
                    vh.tvCount.setBackgroundResource(R.drawable.btn_count_unable);
                } else {
                    vh.tv_difference.setVisibility(View.GONE);
//                vh.tv_difference.setText(Html.fromHtml("还差<font color='#FF9E16'>0</font>元"));
                    vh.tvCount.setEnabled(true);
                    vh.tvCount.setBackgroundResource(R.drawable.btn_count_normal);

                }
            }


        } else {
            vh.tv_starting_account.setVisibility(View.GONE);
            vh.tv_difference.setVisibility(View.GONE);
        }

        return convertView;
    }


//    void refresh(boolean isNeedRefresh,ChildViewHolder vh,CartGoods cartGoods){
//        if (isNeedRefresh){
//            vh.vCount.setVisibility(View.VISIBLE);
//            Pair<Integer, Double> selectTotalPrice = getSelectTotalPrice(cartGoods);
//
//            int selectCount = selectTotalPrice.first;
//
//            double totalPrice = selectTotalPrice.second == null ? 0:selectTotalPrice.second;
//
//            boolean isUnable = false;
//            vh.tvTotal.setText(context.getString(R.string.total_price, totalPrice));
//
//
//
//            if (selectCount > 0){
//                vh.tvCount.setText("结算("+ selectCount+")");
//                vh.tvCount.setBackgroundResource(R.drawable.btn_count_normal);
//                isUnable = false;
//            }else {
//                vh.tvCount.setBackgroundResource(R.drawable.btn_count_unable);
//                vh.tvCount.setText("结算");
//
//                isUnable = true;
//            }
//            if (isEdit){
//                vh.tvCount.setBackgroundResource(R.drawable.btn_count_unable);
//                vh.tvTotalName.setVisibility(View.GONE);
//                vh.tvTotal.setVisibility(View.GONE);
//                vh.tvCount.setText("结算");
//                vh.tvCount.setEnabled(false);
//                isUnable = true;
//            }else {
//
//                vh.tvTotalName.setVisibility(View.VISIBLE);
//                vh.tvTotal.setVisibility(View.VISIBLE);
//                vh.tvCount.setEnabled(true);
//            }
//            vh.tvCount.setTag(R.id.item_key_status,isUnable);
//            vh.tvCount.setTag(R.id.item_key_view,cartGoods);
//            vh.tvCount.setOnClickListener(onCountClickListener);
//        }else {
//            vh.vCount.setVisibility(View.GONE);
//        }
//    }


    void initState(@NonNull ChildViewHolder vh, @NonNull CartGood cartGood) {

        OfferDtoBean bean = cartGood.getOfferProductDto();
        vh.gif_1.setVisibility(View.GONE);
        vh.gif_2.setVisibility(View.GONE);
        vh.gif_3.setVisibility(View.GONE);
        vh.gif_4.setVisibility(View.GONE);
        vh.tv_offer_price.setVisibility(View.GONE);
        vh.tv_offer_hint.setVisibility(View.GONE);
        vh.tv_offer_time_remind.setVisibility(View.GONE);

        switch (bean.getStatus()) {

            //1报价中 2已报价 3放弃报价 4放弃问价(没有？) 5报价失效
            case 1://绿色动图，灰色倒计时 努力报价中
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setTextColor(colors[2]);

                vh.gif_1.setVisibility(View.VISIBLE);
                GifDrawable gifDrawable = (GifDrawable) vh.gif_1.getDrawable();
                gifDrawable.start();
                vh.tv_offer_hint.setVisibility(View.VISIBLE);
                vh.tv_offer_hint.setTextColor(colors[2]);
                vh.tv_offer_hint.setText(bean.getText());
                String buyTime = bean.getOfferBuyRemindTime();
                vh.tv_offer_time_remind.setText(bean.getOfferBuyRemindTime());
                if (buyTime.endsWith("5:00") || buyTime.endsWith("0:00")) {
                    freshItemListener.freshItem(0, 0, cartGood, null);
                }
                if ("00:00".endsWith(buyTime)) {
                    bean.setStatus(3);// TODO: 2018/7/9
                    bean.setText("忙碌，过会问");
                }
                vh.ll_offer.setTag(R.id.item_key_position, 0);
                vh.ll_offer.setTag(R.id.item_key_status, cartGood);
                vh.ll_offer.setOnClickListener(this);
                vh.ll_offer.setVisibility(View.VISIBLE);


//                isShowOfferPrice=false;

                break;
            case 2://5分钟内部红动图，红色倒计时；充裕红圈动图，灰色倒计时 新价格 老价格变黑横线
                vh.ll_offer.setTag(R.id.item_key_position, 0);
                vh.ll_offer.setTag(R.id.item_key_status, cartGood);
                vh.ll_offer.setOnClickListener(this);
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);
                if (bean.getCount() > cartGood.getCount()) {//数量不到    //红 黑 灰
                    vh.tv_offer_price.setTextColor(colors[1]);
                    vh.tv_offer_price.setPaintFlags(vh.tv_offer_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    vh.tv_price.setTextColor(colors[0]);
                    vh.tv_price.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                    if (cartGood.isPrePrice()) {
                        vh.tv_price.setText("¥" + cartGood.getLastestUnitPrice() + "/" + cartGood.getProductChargeUnit());//重新设置显示原价
                        vh.tvUnit.setVisibility(View.VISIBLE);
                    } else {
                        vh.tv_price.setText("时价");//重新设置显示原价
                        vh.tvUnit.setVisibility(View.GONE);
                    }

//                    isShowOfferPrice=false;
                } else {
                    vh.tv_price.setTextColor(colors[1]);
                    vh.tv_price.setPaintFlags(vh.tv_offer_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    vh.tv_offer_price.setTextColor(colors[0]);
                    vh.tv_offer_price.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                    if (cartGood.isPrePrice()) {
                        vh.tv_price.setText("¥" + cartGood.getLastestUnitPrice() + "/" + cartGood.getProductChargeUnit());//重新设置显示原价
                        vh.tvUnit.setVisibility(View.VISIBLE);
                    } else {
                        vh.tv_price.setText("时价");//重新设置显示原价
                        vh.tvUnit.setVisibility(View.GONE);
                    }


//                    isShowOfferPrice=true;
                }
                vh.tv_offer_price.setText(bean.getText());
                vh.tv_offer_price.setVisibility(View.VISIBLE);
                String sellTime = bean.getOfferSellRemindTime();
                vh.tv_offer_time_remind.setText(bean.getOfferSellRemindTime());
                if (bean.isLastBuyFiveFlag()) {
                    vh.gif_3.setVisibility(View.VISIBLE);
                    GifDrawable gifDrawable3 = (GifDrawable) vh.gif_3.getDrawable();
                    gifDrawable3.start();
                    vh.tv_offer_time_remind.setTextColor(colors[0]);
                } else {
                    vh.gif_2.setVisibility(View.VISIBLE);
                    GifDrawable gifDrawable2 = (GifDrawable) vh.gif_2.getDrawable();
                    gifDrawable2.start();
                    vh.tv_offer_time_remind.setTextColor(colors[2]);
                }
                vh.ll_offer.setVisibility(View.VISIBLE);
                if ("00:00".endsWith(sellTime)) {
                    bean.setStatus(5);
                    vh.ll_offer.setVisibility(View.GONE);
                    vh.tv_price.setTextColor(colors[0]);
                    vh.tv_price.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

//                    refresh(true,vh,goodss);
//                    isShowOfferPrice=false;

                }
                /*todo:倒计时 倒计时分两种 5分钟内红色*/
                break;
            case 3://绿色静图 00:00  忙碌，过会问
                vh.ll_offer.setTag(R.id.item_key_position, 0);
                vh.ll_offer.setTag(R.id.item_key_status, cartGood);
                vh.ll_offer.setOnClickListener(this);
                vh.ll_offer.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);
                vh.gif_4.setVisibility(View.VISIBLE);
                vh.tv_offer_hint.setText(bean.getText());
                vh.tv_offer_hint.setTextColor(colors[2]);
                vh.tv_offer_hint.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setText("00:00");

//                isShowOfferPrice=false;
                break;
            case 4://绿色静图 00:00 可能没有该类型
                /*vh.tv_offer_time_remind.setVisibility(View.VISIBLE);
                vh.gif_4.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setText("00:00");
                vh.tv_offer_hint.setTextColor(colors[3]);
                vh.tv_offer_hint.setVisibility(View.VISIBLE);*/
                break;
            case 5://绿色静图 嫌贵？00:00 去问价
/*                vh.tv_offer_time_remind.setVisibility(View.GONE);
                vh.gif_4.setVisibility(View.VISIBLE);
                vh.tv_offer_hint.setText(bean.getText());
                vh.tv_offer_hint.setTextColor(colors[3]);
                vh.tv_offer_hint.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setText("00:00");*/
                break;
        }
    }

    public Pair<Integer, Double> getSelectTotalPrice(CartGoods cartGoods) {
        int cnt = 0;
        double total = 0d;
        if (cartGoods != null) {

            if (isEdit || cartGoods.isDisabled()) {
                return new Pair<>(cnt, total);
            }

            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);
                    if (cartGood.isSelect()) {
                        if (isEdit) {//编辑状态
                            cnt++;
                        } else if (cartGood.isCheckCityId()) {
                            cnt++;
//                            double productUnitPrice = cartGood.getProductUnitPrice();

//                            double productUnitPrice;

//                            if (isShowOfferPrice){
//                                if (cartGood.getOfferProductDto()!=null){//问价后价格
//                                    if(cartGood.getOfferProductDto().getPrice()!=null){
//                                productUnitPrice=Double.valueOf(cartGood.getOfferProductDto().getPrice());
//                                    }else {
//                                        productUnitPrice=cartGood.getLastestUnitPrice();
//                                    }
//                                }else {
//                                    productUnitPrice=cartGood.getLastestUnitPrice();
//                                }
//                            }else if (isSecondKillOrPromotion){//促销或秒杀价
//                                productUnitPrice=cartGood.getProductUnitPrice();
//                            }else {//正常价
//                                productUnitPrice=cartGood.getLastestUnitPrice();
//                            }


                            double productUnitPrice = cartGood.getProductUnitPrice();

                            double productCount = cartGood.getProductCount();
//                            total += productUnitPrice * productCount;
                            BigDecimal bd = new BigDecimal(total);
                            bd = bd.add(new BigDecimal(productUnitPrice).multiply(new BigDecimal(productCount)));
                            total = bd.setScale(2, RoundingMode.HALF_UP).doubleValue();
//                            total = FormatUtil.countMultiply(productUnitPrice , productCount);
                        }

                    }
                }
            }
        }


        return new Pair<>(cnt, total);
    }

    public float mRawX;
    public float mRawY;
    ExpandableListView.OnChildClickListener onChildClickListener;
    ExpandableListView.OnChildClickListener onChildLongClickListener;

    public ExpandableListView.OnChildClickListener getOnChildLongClickListener() {
        return onChildLongClickListener;
    }

    public void setOnChildLongClickListener(ExpandableListView.OnChildClickListener onChildLongClickListener) {
        this.onChildLongClickListener = onChildLongClickListener;
    }

    public ExpandableListView.OnChildClickListener getOnChildClickListener() {
        return onChildClickListener;
    }

    public void setOnChildClickListener(ExpandableListView.OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    private View.OnClickListener onCountClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Object o1 = view.getTag(R.id.item_key_status);
            Object o2 = view.getTag(R.id.item_key_view);

//            vh.tvCount.setTag(R.id.item_key_status,isUnable);
//            vh.tvCount.setTag(R.id.item_key_view,cartGoods);

            if (onCountListener != null && o1 != null && o2 != null && o1 instanceof Boolean && o2 instanceof CartGoods) {
                onCountListener.onCountListener((CartGoods) o2, (Boolean) o1);
            }


        }
    };
    private View.OnClickListener onMoreClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Object o2 = view.getTag(R.id.item_key_view);
//            vh.tvCount.setTag(R.id.item_key_status,isUnable);
//            vh.tvCount.setTag(R.id.item_key_view,cartGoods);

            if (o2 != null && o2 instanceof CartGoods) {
                CartGoods cartGoods7 = (CartGoods) o2;
                boolean expend = cartGoods7.isExpend();
                cartGoods7.setExpend(!expend);
                notifyDataSetChanged();
            }


        }
    };
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Object o1 = view.getTag(R.id.item_key_position);
            Object o2 = view.getTag(R.id.item_key_status);

            int groupPosition = (int) o1;
            int childPosition = (int) o2;
            if (onChildClickListener != null) {
                onChildClickListener.onChildClick(lv, view, groupPosition, childPosition, groupPosition);
            }

        }
    };
    private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            Object o1 = view.getTag(R.id.item_key_position);
            Object o2 = view.getTag(R.id.item_key_status);

            int groupPosition = (int) o1;
            int childPosition = (int) o2;
            if (onChildLongClickListener != null) {
                onChildLongClickListener.onChildClick(lv, view, groupPosition, childPosition, groupPosition);
            }

            return true;
        }
    };

    public interface OnMsgAddListener {
        void onMsgAdd(int position, CartGood goods);
    }

    OnMsgAddListener onMsgAddListener;

    public OnMsgAddListener getOnMsgAddListener() {
        return onMsgAddListener;
    }

    public void setOnMsgAddListener(OnMsgAddListener onMsgAddListener) {
        this.onMsgAddListener = onMsgAddListener;
    }

    @Override
    public void onClick(View v) {


        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;

            switch (v.getId()) {
                case R.id.tv_offer:
                    Intent more_activity = new Intent(v.getContext(),
                            OfferListActivity.class);
                    more_activity.putExtra("marketId", groupList.get(position).getWholeSaleMarketId());
                    v.getContext().startActivity(more_activity);
                    break;
                case R.id.ll_offer:
                    CartGood offerGoods = (CartGood) v.getTag(R.id.item_key_status);
                    if (offerGoods != null && offerGoods.getOfferProductDto() != null) {
                        if (offerGoods.getOfferProductDto().getStatus() == 1 || offerGoods.getOfferProductDto().getStatus() == 2)
                            T.toast("要货" + offerGoods.getOfferProductDto().getCount() + offerGoods.getProductChargeUnit());
                    }
                    break;
                case R.id.tv_msg:
                    Object o = v.getTag(R.id.item_key_status);

                    if (onMsgAddListener != null && o instanceof CartGood) {
                        onMsgAddListener.onMsgAdd(position, (CartGood) o);
                    }
                    break;

                case R.id.iv_box:
                    CartGoods cartGoods = groupList.get(position);
                    cartGoods.setSelect(!cartGoods.isSelect());
                    checkBoxState(cartGoods.isSelect(), (ImageView) v);

                    checkAllChildStatus(cartGoods.getResult(), cartGoods.isSelect());

                    if (onCheckChangeListener != null) {
                        onCheckChangeListener.onCheckChange(checkAllStatus(), position, -1);
                    }
                    break;

                case R.id.iv_child_box:
                    Object tag1 = v.getTag(R.id.item_key_status);

                    if (tag1 instanceof Integer) {
                        int childPosition = (int) tag1;

                        CartGoods cartGoods1 = groupList.get(position);
                        List<CartGood> goods = cartGoods1.getResult();

                        CartGood cartGood = goods.get(childPosition);
                        cartGood.setSelect(!cartGood.isSelect());

                        checkBoxState(cartGood.isSelect(), (ImageView) v);
                        //如果子类变为非选中状态,则更新父类状态
                        if (!cartGood.isSelect()) {
                            cartGoods1.setSelect(false);
                            notifyDataSetChanged();
                        } else {
                            checkParentStatus(cartGoods1, goods);
                        }
                        if (onCheckChangeListener != null) {
                            onCheckChangeListener.onCheckChange(checkAllStatus(), position, childPosition);
                        }
                    }
                    break;
                case R.id.iv_add:// 加购物车
                    Object tag2 = v.getTag(R.id.item_key_status);

                    if (tag2 instanceof Integer) {
                        int childPosition = (int) tag2;

                        TextView tv = (TextView) v.getTag(R.id.item_key_view);


                        CartGoods cartGoods1 = groupList.get(position);
                        List<CartGood> goods = cartGoods1.getResult();
                        if (childPosition >= goods.size()) {
                            return;
                        }
                        CartGood cartGood = goods.get(childPosition);

                        double amount = cartGood.getProductCount();
                        double lastAmount = cartGood.getProductCount();
                        double min = cartGood.getMinPlaceOrderValue();
                        double incr = cartGood.getIncr();
                        if (amount < min) {
                            amount = min;
                        }

                        amount = FormatUtil.countAdd(amount, incr);
                        if (cartGood.getWholeSalesType() == 2) {//散货

                        } else {
                            if (cartGood.getMaxPlaceOrderValue() < amount) {
                                T.toast("超过库存");
                                amount = FormatUtil.countSubtract(amount, incr);
                            }
                        }
                        cartGood.setProductCount(amount);
                        tv.setText(FormatUtil.subZeroAndDot(amount + ""));

                        if (onChangeListener != null) {
                            onChangeListener.onTextChange(position, childPosition, lastAmount, cartGood);
                        }

                    }
                    break;
                case R.id.iv_remove://"减购物车"
                    Object tag3 = v.getTag(R.id.item_key_status);

                    if (tag3 instanceof Integer) {
                        int childPosition = (int) tag3;

                        TextView tv = (TextView) v.getTag(R.id.item_key_view);


                        CartGoods cartGoods1 = groupList.get(position);
                        List<CartGood> goods = cartGoods1.getResult();
                        CartGood cartGood = goods.get(childPosition);

                        double amount = cartGood.getProductCount();
                        double lastAmount = cartGood.getProductCount();
                        double min = cartGood.getMinPlaceOrderValue();
                        double incr = cartGood.getIncr();
                        if (amount < min) {
                            amount = min;
                        }
                        if (cartGood.getWholeSalesType() == 2) {//散货
                            amount = FormatUtil.countSubtract(amount, incr);
                            if (amount < min) {
                                deleteOne.deleteOne(position, childPosition);
                                return;
                                // amount = min;
                            }
                        } else {
                            if (amount > cartGood.getMinPlaceOrderValue())
                                amount = FormatUtil.countSubtract(amount, incr);
                            else {
                                deleteOne.deleteOne(position, childPosition);
                                return;

                                //T.toast("购买数量不能小于起订量");
                            }
                            if (amount < min) {
                                amount = min;
                            }
                        }
                        cartGood.setProductCount(amount);
                        tv.setText(FormatUtil.subZeroAndDot(amount + ""));
                        if (onChangeListener != null) {
                            onChangeListener.onTextChange(position, childPosition, lastAmount, cartGood);
                        }
                    }
                    break;
                case R.id.ed_price:
                    Object tag4 = v.getTag(R.id.item_key_status);

                    if (tag4 instanceof Integer) {
                        int childPosition = (int) tag4;

                        TextView tv = (TextView) v.getTag(R.id.item_key_view);


                        CartGoods cartGoods1 = groupList.get(position);
                        List<CartGood> goods = cartGoods1.getResult();
                        CartGood cartGood = goods.get(childPosition);

                        if (onTextChangeListener != null) {
                            onTextChangeListener.onTextChange(position, childPosition, cartGood.getProductCount(), cartGood);
                        }

                    }
                    break;

            }


        }
    }


    public void editState() {
        for (int i = 0; i < groupList.size(); i++) {
            CartGoods cartGoods = groupList.get(i);
            if (cartGoods != null) {
                List<CartGood> result = cartGoods.getResult();
                if (result != null) {
                    for (int j = 0; j < result.size(); j++) {
                        CartGood cartGood = result.get(j);
                        cartGood.storedSelect();
                    }
                    checkParentStatus(cartGoods, result);
                }
            }
        }


    }

    public void releaseStatus() {
        for (int i = 0; i < groupList.size(); i++) {
            CartGoods cartGoods = groupList.get(i);
            if (cartGoods != null) {
                List<CartGood> result = cartGoods.getResult();
                if (result != null) {
                    for (int j = 0; j < result.size(); j++) {
                        CartGood cartGood = result.get(j);
                        cartGood.releaseSelect();
                    }
                    checkParentStatus(cartGoods, result);
                }
            }
        }


    }


    public void checkBoxState(boolean isCheck, ImageView iv) {
        if (isCheck) {
            //全选
            iv.setImageResource(R.drawable.select_round);
        } else {
            //反选
            iv.setImageResource(R.drawable.register_unread);
        }
    }

    public void checkAllChildStatus(List<CartGood> list, boolean isCheck) {

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                CartGood cartGood = list.get(i);
                cartGood.setSelect(isCheck);
            }
            notifyDataSetChanged();
        }
    }

    public void checkParentStatus(CartGoods cartGoods, List<CartGood> list) {

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                CartGood cartGood = list.get(i);
                if (isEdit) {
                    if (!cartGood.isSelect()) {
                        cartGoods.setSelect(false);
                        notifyDataSetChanged();
                        return;
                    }
                } else if (cartGood.isCheckCityId()) {
                    if (!cartGood.isSelect()) {
                        cartGoods.setSelect(false);
                        notifyDataSetChanged();
                        return;
                    }
                } else if (!cartGood.isCheckCityId()) {
                    cartGoods.setSelect(false);
                    notifyDataSetChanged();
                    return;
                }

            }
            cartGoods.setSelect(true);
        } else {
            cartGoods.setSelect(false);
        }
        notifyDataSetChanged();
    }

    public void checkStatus() {

        for (int i = 0; i < groupList.size(); i++) {
            CartGoods cartGoods = groupList.get(i);
            List<CartGood> result = cartGoods.getResult();
            checkParentStatus(cartGoods, result);
        }
    }

    public void checkAllStatus(boolean isCheck) {
        for (int j = 0; j < groupList.size(); j++) {

            CartGoods cartGoods = groupList.get(j);
            if (!isEdit && cartGoods.isDisabled()) {
                cartGoods.setSelect(false);
                continue;
            }
            cartGoods.setSelect(isCheck);
            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);

                    if (isEdit) {
                        cartGood.setSelect(isCheck);
                    } else {

                        if (cartGood.isCheckCityId()) {
                            cartGood.setSelect(isCheck);
                        } else {
                            cartGood.setSelect(false);

                        }
                    }
                }
            }
        }
        notifyDataSetChanged();

    }

    public boolean checkAllStatus() {
        boolean isCheck = true;
        boolean isOnlyDisabledCartGoods = true;
        for (int j = 0; j < groupList.size(); j++) {

            CartGoods cartGoods = groupList.get(j);

            if (!isEdit && cartGoods.isDisabled()) {
                continue;
            }
            isOnlyDisabledCartGoods = false;
            if (!cartGoods.isSelect()) {
                return false;
            }
            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);
                    if (isEdit && !cartGood.isSelect()) {
                        return false;
                    } else if (!cartGood.isCheckCityId()) {

                    } else if (!cartGood.isSelect()) {
                        return false;
                    }
                }
            }
        }

        return isCheck && (!isOnlyDisabledCartGoods);
    }

    public Pair<Integer, Double> calAllCheckedStatus() {
        int cnt = 0;
        double total = 0d;
        for (int j = 0; j < groupList.size(); j++) {

            CartGoods cartGoods = groupList.get(j);

            if (!isEdit && cartGoods.isDisabled()) {
                continue;
            }

            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);
                    if (cartGood.isSelect()) {
                        if (isEdit) {//编辑状态
                            cnt++;
                        } else if (cartGood.isCheckCityId()) {
                            cnt++;
                            double productUnitPrice = cartGood.getProductUnitPrice();
                            double productCount = cartGood.getProductCount();
//                            total += productUnitPrice * productCount;
                            BigDecimal bd = new BigDecimal(total);
                            bd = bd.add(new BigDecimal(productUnitPrice).multiply(new BigDecimal(productCount)));
                            total = bd.setScale(2, RoundingMode.HALF_UP).doubleValue();
//                            total = FormatUtil.countMultiply(productUnitPrice , productCount);
                        }

                    }
                }
            }
        }

        return new Pair<>(cnt, total);
    }

    public List<String> getAllCheckedStatus() {
        List<String> listChecked = new ArrayList<>();
        for (int j = 0; j < groupList.size(); j++) {

            CartGoods cartGoods = groupList.get(j);

            if (!isEdit && cartGoods.isDisabled()) {
                continue;
            }

            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);
                    if (cartGood.isSelect()) {

                        if (isEdit) {//编辑状态
                            listChecked.add(cartGood.getId() + "");
                        } else if (cartGood.isCheckCityId()) {
                            listChecked.add(cartGood.getId() + "");
                        }

                    }
                }
            }
        }

        return listChecked;
    }

    public List<String> getAllDeleteCheckedStatus() {
        List<String> listChecked = new ArrayList<>();
        for (int j = 0; j < groupList.size(); j++) {

            CartGoods cartGoods = groupList.get(j);

            if (!isEdit && cartGoods.isDisabled()) {
                continue;
            }

            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);
                    if (cartGood.isSelect()) {

                        if (isEdit) {//编辑状态
                            listChecked.add(cartGood.getId() + "");
                        } else if (cartGood.isCheckCityId()) {
                            listChecked.add(cartGood.getId() + "");
                        }

                    }
                }
            }
        }

        return listChecked;
    }

    public List<Map<String, Object>> getSelectedStatus(int groupPosition) {
        List<Map<String, Object>> listChecked = new ArrayList<>();
        if (groupPosition < groupList.size()) {
            CartGoods cartGoods = groupList.get(groupPosition);
            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);

//                        "id": "1", "isSelected":"1" //1:选中; 0:选中
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cartGood.getId());
                    map.put("isSelected", cartGood.getIsSelected());

                    listChecked.add(map);

                }
            }

        }
        return listChecked;
    }

    public List<Map<String, Object>> getAllSelectedStatus() {
        List<Map<String, Object>> listChecked = new ArrayList<>();
        for (int j = 0; j < groupList.size(); j++) {

            CartGoods cartGoods = groupList.get(j);


            List<CartGood> list = cartGoods.getResult();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    CartGood cartGood = list.get(i);

//                        "id": "1", "isSelected":"1" //1:选中; 0:选中
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cartGood.getId());
                    map.put("isSelected", cartGood.getIsSelected());

                    listChecked.add(map);

                }
            }
        }

        return listChecked;
    }

    public List<Map<String, Object>> getSelectedStatus(int groupPosition, int childPosition) {
        List<Map<String, Object>> list = new ArrayList<>();

        //全部选中或者全部不选中
        if (groupPosition < 0) {
            return getAllSelectedStatus();

        } else if (childPosition < 0 && groupPosition < groupList.size()) {//选中/不选中某个市场的
            return getSelectedStatus(groupPosition);
        } else if (groupPosition < groupList.size() && groupList.get(groupPosition).getResult() != null && childPosition < groupList.get(groupPosition).getResult().size()) {
            //选中/不选中具体某个
            CartGoods cartGoods = groupList.get(groupPosition);
            List<CartGood> result = cartGoods.getResult();
            if (result != null) {
                CartGood cartGood = result.get(childPosition);
                if (cartGood != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cartGood.getId());
                    map.put("isSelected", cartGood.getIsSelected());
                    list.add(map);
                }
            }
        }


        return list;
    }


    /**
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    protected void expendList() {

        if (lv != null)
            for (int i = 0; i < groupList.size(); i++) {
                lv.expandGroup(i);
            }
    }

    public class EdUnitWatch implements TextWatcher {

        EditText ed;

        public EdUnitWatch(EditText ed) {
            this.ed = ed;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            int groupPosition = (int) ed.getTag(R.id.item_key_position);
            int childPosition = (int) ed.getTag(R.id.item_key_status);

            if (groupList.size() > groupPosition) {
                CartGoods cartGoods = groupList.get(groupPosition);
                List<CartGood> goods = cartGoods.getResult();
                if (goods != null && goods.size() > childPosition) {
                    CartGood cartGood = goods.get(childPosition);


                    double amount = 0;
                    if (s.length() > 0) {
                        amount = Integer.valueOf(s.toString());
                    } else {
                        amount = cartGood.getProductCount();
                    }

                    if (amount < cartGood.getMinPlaceOrderValue()) {
                        amount = cartGood.getMinPlaceOrderValue();
                    }


                    cartGood.setProductCount(amount);
                }


            }

        }
    }

    public class ViewHolder {

        ImageView iv;
        TextView tv;
        View vLine;

        TextView tvOffer;

    }

    public class ChildViewHolder {
        TextView tvStore;

        TextView tv_car_offer_hint;

        ImageView ivChildCheck;
        ImageView ivLogo;
        //现货,预售标志
        TextView tvType;
        TextView tvTitle;
        TextView tvOutOfDate;
        TextView tvOutOfCity;
        TextView tvChargeNumber;
        ImageView ivOutOfCity;

        TextView tvUnit;
        //TextView tvUnitPrice;

        ImageView ivAdd;
        ImageView ivRemove;
        LinearLayout edAmountUnderlineLayout;
        TextView edAmountUnderlineTv;
        TextView edAmount;

        //View vLine;
        View vBacker;

        /*        TextView tvDiscount;
                TextView tvDiscountTitle;
                TextView tvDiscountRate;*/
        TextView tvDisable;

        //TextView tvPromotion;
//        TextView tvTag1;
//        TextView tvTag2;
        ImageView ivCorner;
        View vCount;

        TextView tvTotalName;
        TextView tvTotal;
        TextView tvCount;
        View vMore;
        TextView tvMore;
        ImageView ivMore;


        View vLine1;
        TextView tvMsg;
        View vLine;
        View vFlag;
        View vArrow;

        TextView tv_price, tv_price_, tv_price_old, tv_activity_promotion, tv_activity_second;

        TagCloudView tag;


        View ll_offer;
        GifImageView gif_1;
        GifImageView gif_2;
        GifImageView gif_3;
        ImageView gif_4;
        TextView tv_offer_hint;
        TextView tv_offer_time_remind;
        TextView tv_offer_price;

        TextView tv_starting_account, tv_difference;
    }


    public interface OnBottomClickListener {

        public void onBottomClickListener(int position, int type);
    }

    public interface OnNotifyDataSetChangedListener {

        public void OnNotifyDataSetChangedListener(int size);
    }

    public interface OnCountListener {

        public void onCountListener(CartGoods cartGoods, boolean isUnable);
    }

    DeleteOne deleteOne;

    public interface DeleteOne {
        public void deleteOne(int deleteOne, int childPosition);
    }

    public DeleteOne getDeleteOne() {
        return deleteOne;
    }

    public void setDeleteOne(DeleteOne deleteOne) {
        this.deleteOne = deleteOne;
    }
}
